package com.common.filter;

import android.text.InputFilter;
import android.text.Spanned;

/**
 * 达到长度后，尾部显示省略号的过滤器
 * 
 * <br>
 * Created 2014-9-10 下午5:16:53
 * 
 * @version
 * @author HuangYK
 * 
 * @see
 */
public class LengthFilterWithEllipsis implements InputFilter {

	/** 最大长度 */
	private int mMax;

	/**
	 * 实例化达到长度后，尾部显示省略号的过滤器
	 * 
	 * Creates a new instance of LengthFilterWithEllipsis. <br>
	 * Created 2014-9-10 下午5:17:29
	 * 
	 * @param max
	 *            最大长度
	 */
	public LengthFilterWithEllipsis(int max) {
		mMax = max;
	}

	@Override
	public CharSequence filter(CharSequence source, int start, int end,
			Spanned dest, int dstart, int dend) {

		if (source.length() > mMax) {
			source = source.subSequence(0, mMax) + "...";
		}

		return source;
	}

}
